﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevFx.Web.Code;
using DevFx.FrameWork;
using DevFx.Model;
using DevFx.FrameWork.Exception;
using DevFx.Biz;

namespace DevFx.Web.Pages.Rights
{
    public partial class UserRole : BasePage
    {
        #region 变量
        private RoleBiz Rbiz = new RoleBiz();
        private UserBiz Ubiz = new UserBiz();
        private UserRoleBiz URbiz = new UserRoleBiz();
        #endregion

        #region 属性
        /// <summary>
        /// 分页对象
        /// </summary>
        protected PagerCondition pager
        {
            get
            {
                if (ViewState["pager"] == null)
                    ViewState["pager"] = new PagerCondition();
                return ViewState["pager"] as PagerCondition;
            }
            set
            {
                ViewState["pager"] = value;
            }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindData();
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SysUserRole role = new SysUserRole();
            role.UserID = EditID;
            role.RoleID = new Guid(this.ddlRoleList.SelectedItem.Value);
            role.UserRoleID = Guid.NewGuid();
            role.CreateDate = DateTime.Now;
            role.CreateBy = UserID;
            role.ModifyBy = UserID;
            role.ModifyDate = DateTime.Now;
            URbiz.Create(role);
            BindGvList();
        }
        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            PushSort(e.SortExpression.ToString(), pager);
            BindGvList();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btndel_Click(object sender, EventArgs e)
        {
            var list = new List<SysUserRole>();
            foreach (GridViewRow rows in this.gvList.Rows)
            {
                CheckBox cbx1 = rows.FindControl("cbx1") as CheckBox;
                HiddenField hfID = rows.FindControl("hfID") as HiddenField;
                if (cbx1.Checked)
                {
                    Guid gid = new Guid(hfID.Value);
                    var t = URbiz.GetByCondition(q => q.UserRoleID == gid);
                    list.Add(t);
                }
            }
            URbiz.Delete(list);
            BindData();
            MessageBox.Show(this.Page, "删除成功,"+"共删除"+list.Count+"条记录！");
        }
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string roleUserID = e.CommandArgument.ToString();
                var t = URbiz.GetByCondition(q => q.UserRoleID == new Guid(roleUserID));
                URbiz.Delete(t);
                BindData();
                MessageBox.Show(this.Page, "删除成功！");
            }

        }
        #endregion

        #region 方法
        private void BindData()
        {
            var user = Ubiz.GetByCondition(u => u.UserID == EditID);
            if (user != null)
            {
                this.lblName.Text = user.UserName;
            }
            else
            {
                MessageBox.Show(this.Page, "参数错误");
                return;
            }
            BindDDLList();
            BindGvList();
        }
        private void BindDDLList()
        {
            //绑定角色列表
            this.ddlRoleList.DataTextField = "RoleName";
            this.ddlRoleList.DataValueField = "RoleID";
            this.ddlRoleList.DataSource = Rbiz.GetList();
            this.ddlRoleList.DataBind();

        }
        private void BindGvList()
        {
            BasePagerInit(pager, "RoleID");
            List<SysUserRole2> list = null;
            IQueryable<SysUserRole2> EntityList = URbiz.getListByUserID(EditID);
            EntityList = LinqHelper.SortingAndPaging(EntityList, pager);
            list = EntityList.ToList();
            this.gvList.DataSource = list;
            this.gvList.DataBind();

        }

        #endregion

    }
}